package com.ming.mallchat.common.common.event.listener;

import com.ming.mallchat.common.common.event.UserOnlineEvent;
import com.ming.mallchat.common.user.dao.UserDao;
import com.ming.mallchat.common.user.domain.enums.UserActiveStatusEnum;
import com.ming.mallchat.common.user.pojo.User;
import com.ming.mallchat.common.user.service.IpService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;
import org.springframework.transaction.event.TransactionPhase;
import org.springframework.transaction.event.TransactionalEventListener;

@Component
public class UserOnlineListener {

    @Autowired
    private UserDao userDao;

    @Autowired
    private IpService ipService;

    /**
     * 更新用户信息
     * @param event 用户上线事件
     */
    @EventListener(classes = UserOnlineEvent.class)
    public void saveDB(UserOnlineEvent event){
        User user = event.getUser();
        user.setLastOptTime(user.getLastOptTime());
        user.setIpInfo(user.getIpInfo());
        user.setActiveStatus(UserActiveStatusEnum.ONLINE.getType());
        userDao.updateById(user);
        //用户ip详情解析
        ipService.refreshIpDetailAsync(user.getId());
    }

}
